package com.gy.hsim.bservice.ecomInfo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.gy.hsim.bservice.ecomInfo.bean.SaleAndOperatorRelation;
import com.gy.hsim.bservice.ecomInfo.bean.UserGroup;
import com.gy.hsim.bservice.ecomInfo.service.EcomInfoService;
import com.gy.hsim.bservice.framework.mapper.SaleRelationMapper;
import com.gy.hsim.bservice.framework.mapper.UserGroupRelationMapper;

/**
 * @ClassName: EcomInfoServiceImpl
 * @Description: 查询 电商相关信息业务实现类
 * @author quanrj
 * @date 2016-03-01 上午09:37:19
 */
@Scope("prototype")
@Service("ecomInfoService")
public class EcomInfoServiceImpl implements EcomInfoService{
	
	@Autowired
	private SaleRelationMapper saleRelationMapper;
	
	@Autowired
	private UserGroupRelationMapper userGroupRelationMapper;
	
	/**
		角色ID（可以有多个角色）。11：系统管理员；0002和0007：店铺管理员（托管企业和成员企业）；0003和0008：收银员（托管企业和成员企业）；0004和0009：服务员（托管企业和成员企业）；0005和0010：送餐员（托管企业和成员企业）
	*/
	//角色id常量
	public static final String ROLEID_11 = "11";
	public static final String ROLEID_0002 = "0002";
	public static final String ROLEID_0007 = "0007";
	public static final String ROLEID_0003 = "0003";
	public static final String ROLEID_0008 = "0008";
	public static final String ROLEID_0004 = "0004";
	public static final String ROLEID_0009 = "0009";
	public static final String ROLEID_0005 = "0005";
	public static final String ROLEID_0010 = "0010";
	
	//角色名称常量
	public static final String ROLENAME_11 = "系统管理员";
	public static final String ROLENAME_0002 = "店铺管理员";
	public static final String ROLENAME_0007 = "店铺管理员";
	public static final String ROLENAME_0003 = "收银员";
	public static final String ROLENAME_0008 = "收银员";
	public static final String ROLENAME_0004 = "服务员";
	public static final String ROLENAME_0009 = "服务员";
	public static final String ROLENAME_0005 = "送餐员";
	public static final String ROLENAME_0010 = "送餐员";
	
	@Override
	public List<SaleAndOperatorRelation> queryAllOperatorBySale_network(
			String enterprise, String sale_network) throws Exception {
		
		List<SaleAndOperatorRelation> list = new ArrayList<SaleAndOperatorRelation>();				
		list = saleRelationMapper.queryOprerators(enterprise, sale_network);				
		return list;
	}

	@Override
	public List<UserGroup> getDesUids(String groupId) {
		// TODO Auto-generated method stub
		List<UserGroup> list = new ArrayList<UserGroup>();
		list = userGroupRelationMapper.getDesUids(groupId);
		return list;
	}

	@Override
	public List<SaleAndOperatorRelation> queryOperatorInfo(String enterprise,
			String operator) throws Exception {
		List<SaleAndOperatorRelation> list = new ArrayList<SaleAndOperatorRelation>();
		list = saleRelationMapper.queryOperatorInfo(enterprise, operator);
		//操作员角色转成角色名
		if(list!=null && list.size()>0){
			for(SaleAndOperatorRelation saleAndOperatorRelation : list){
				String roleId = saleAndOperatorRelation.getRoleid();
				Map<String, String> getRoleidToRoleNameMap = getRoleidToRoleNameMap();
				String roleName = "";
				if(roleId != null && !roleId.equals("")){
					String[] roleIdArr = roleId.split(",");
					for(int i = 0; i< roleIdArr.length; i++){
						String subRoleName = getRoleidToRoleNameMap.get(roleIdArr[i]);
						if(subRoleName != null){
							roleName = roleName + subRoleName +",";
						}
					}
				}
				if(!roleName.equals("")){
					roleName = roleName.substring(0,roleName.length()-1);
				}
				saleAndOperatorRelation.setRoleName(roleName);
			}
		}
		return list;
	}
	
	/**存储所有角色ID和角色名称的对照信息*/
	public Map<String, String> getRoleidToRoleNameMap(){
		Map<String, String> roleidToRoleNameMap = new HashMap<String, String>();
			
		roleidToRoleNameMap.put(ROLEID_11, ROLENAME_11);
		roleidToRoleNameMap.put(ROLEID_0002, ROLENAME_0002);
		roleidToRoleNameMap.put(ROLEID_0007, ROLENAME_0007);
		roleidToRoleNameMap.put(ROLEID_0003, ROLENAME_0003);
		roleidToRoleNameMap.put(ROLEID_0008, ROLENAME_0008);
		roleidToRoleNameMap.put(ROLEID_0004, ROLENAME_0004);
		roleidToRoleNameMap.put(ROLEID_0009, ROLENAME_0009);
		roleidToRoleNameMap.put(ROLEID_0005, ROLENAME_0005);
		roleidToRoleNameMap.put(ROLEID_0010, ROLENAME_0010);
				
		return roleidToRoleNameMap;
	}
	
}
